package uoa.assignment.game;

import java.util.Objects;
import java.util.Scanner;

import uoa.assignment.character.GameCharacter;
import uoa.assignment.character.Player;
import uoa.assignment.character.Monster;

public class GameLogic {

	public static void moveCharacter(String input, Map gameMap, GameCharacter character) {
		if(!(input.equals("up")||input.equals("right")||input.equals("left")||input.equals("down")))
		{
			System.out.print("Use only keywords up, down, left, right\n");
		}
		else if(input.equals("up"))
		{
			moveUp(character,gameMap);
		}
		else if(input.equals("right"))
		{
			moveRight(character, gameMap);
		}
		else if(input.equals("left"))
		{
			moveLeft(character, gameMap);
		}
		else
		{
			moveDown(character, gameMap);
		}
		
	}
	private static void moveRight(GameCharacter character, Map gameMap)
	{


		int height=gameMap.layout.length;
		int weight=gameMap.layout[0].length;
		if(character.column==weight-1)
		{
			System.out.println("You can't go right. You lose a move");
			return ;
		}
		String input="";
		if(gameMap.layout[character.row][character.column+1].equals("x"))
		{
			return;
		}
		if(Objects.equals(character.sayName(), "player"))
		{
			input="*";
		}
		else
		{
			input="%";
		}
		if(input.equals("%")&&(gameMap.layout[character.row][character.column+1].equals("%")))
		{
			System.out.println("Monster already there so can't move");
			return;
		}
		if(input.equals("%")&&(gameMap.layout[character.row][character.column+1].equals("*")))
		{
			character.hurtCharacter(gameMap.characters[0]);
			return;
		}
		if(input.equals("*")&&(gameMap.layout[character.row][character.column+1].equals("%")))
		{
			GameCharacter target = null;
			for(GameCharacter ch:gameMap.characters)
			{

				if(ch.row==character.row&&ch.column==character.column+1)
				{
					target=ch;
				}
			}
			if(target.getHealth()<=0)
			{	gameMap.layout[target.row][target.column]="x";
				System.out.println("Character already dead");
				return;
			}
			character.hurtCharacter(target);
			return;
		}
		gameMap.layout[character.row][character.column]=".";
		character.column+=1;
		gameMap.layout[character.row][character.column]=input;
	}
	private static void moveLeft(GameCharacter character, Map gameMap)
	{

		int height=gameMap.layout.length;
		int weight=gameMap.layout[0].length;
		if(character.column==0)
		{
			System.out.println("You can't go left. You lose a move");
			return ;
		}
		String input="";
		if(Objects.equals(character.sayName(), "player"))
		{
			input="*";
		}
		else
		{
			input="%";
		}
		if(gameMap.layout[character.row][character.column-1].equals("x"))
		{
			return;
		}
		if(input.equals("%")&&(gameMap.layout[character.row][character.column-1].equals("%")))
		{
			System.out.println("Monster already there so can't move");
			return;
		}
		if(input.equals("%")&&(gameMap.layout[character.row][character.column-1].equals("*")))
		{
			character.hurtCharacter(gameMap.characters[0]);

			return;
		}
		if(input.equals("*")&&(gameMap.layout[character.row][character.column-1].equals("%")))
		{
			GameCharacter target = null;
			for(GameCharacter ch:gameMap.characters)
			{

				if(ch.row==character.row&&ch.column==character.column-1)
				{
					target=ch;
				}
			}
			if(target.getHealth()<=0)
			{
				gameMap.layout[target.row][target.column]="x";
				System.out.println("Character already dead");
				return;
			}
			character.hurtCharacter(target);
			return;
		}
		gameMap.layout[character.row][character.column]=".";
		character.column-=1;
		gameMap.layout[character.row][character.column]=input;
	}
	private static void moveUp(GameCharacter character, Map gameMap)
	{
		int height=gameMap.layout.length;
		int weight=gameMap.layout[0].length;
		if(character.row==0)
		{
			System.out.println("You can't go up. You lose a move");
			return ;
		}
		String input="";
		if(Objects.equals(character.sayName(), "player"))
		{
			input="*";
		}
		else
		{
			input="%";
		}
		if(gameMap.layout[character.row-1][character.column].equals("x"))
		{
			return;
		}
		if(input.equals("%")&&(gameMap.layout[character.row-1][character.column].equals("%")))
		{
			System.out.println("Monster already there so can't move");
			return;
		}
		if(input.equals("%")&&(gameMap.layout[character.row-1][character.column].equals("*")))
		{
			character.hurtCharacter(gameMap.characters[0]);
			return;
		}
		if(input.equals("*")&&(gameMap.layout[character.row-1][character.column].equals("%")))
		{
			GameCharacter target = null;
			for(GameCharacter ch:gameMap.characters)
			{

				if(ch.row==character.row-1&&ch.column==character.column)
				{
					target=ch;
				}
			}
			if(target.getHealth()<=0)
			{
				gameMap.layout[target.row][target.column]="x";
				System.out.println("Character already dead");
				return;
			}
			character.hurtCharacter(target);
			return;
		}
		gameMap.layout[character.row][character.column]=".";
		character.row-=1;
		gameMap.layout[character.row][character.column]=input;
	}
	private static void moveDown(GameCharacter character, Map gameMap)
	{

		int height=gameMap.layout.length;
		int weight=gameMap.layout[0].length;

		if(character.row==height-1)
		{
			System.out.println("You can't go down. You lose a move");
			return ;
		}
		String input;
		if(Objects.equals(character.sayName(), "player"))
		{
			input="*";
		}
		else
		{
			input="%";
		}
		if(gameMap.layout[character.row+1][character.column].equals("x"))
		{
			return;
		}
		if(input.equals("%")&&(gameMap.layout[character.row+1][character.column].equals("%")))
		{
			System.out.println("Monster already there so can't move");
			return;
		}
		if(input.equals("%")&&(gameMap.layout[character.row+1][character.column].equals("*")))
		{
			character.hurtCharacter(gameMap.characters[0]);
			return;
		}
		if(input.equals("*")&&(gameMap.layout[character.row+1][character.column].equals("%")))
		{
			GameCharacter target = null;
			for(GameCharacter ch:gameMap.characters)
			{

				if(ch.row==character.row+1&&ch.column==character.column)
				{
					target=ch;
				}
			}
			if(target.getHealth()<=0)
			{
				gameMap.layout[target.row][target.column]="x";
				System.out.println("Character already dead");
				return;
			}
			character.hurtCharacter(target);

			return;
		}
		gameMap.layout[character.row][character.column]=".";
		character.row+=1;
		gameMap.layout[character.row][character.column]=input;
	}
}
